/**
 * File Name   : InfoModifyAction.java
 * Description : 
 */
package com.hh.admin.infochg.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.springframework.web.struts.ActionSupport;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.infochg.form.InfoModifyForm;
import com.hh.admin.infochg.service.InfoModifyService;
import com.hh.admin.login.service.LoginService;
import com.hh.common.consts.AdminConst;

/**
 * 后台管理员用户修改个人信息
 * 
 * @author 冯昌硕
 * @create 2012-12-17 下午11:19:57
 */
public class InfoModifyAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(InfoModifyAction.class);
    
    private InfoModifyService infoModifyService;
    
    private LoginService loginService;
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        log.debug("后台用户修改个人信息 begin");
        InfoModifyForm infoModifyForm = (InfoModifyForm) form;
        
        // 取表单数据
        String oldPwd = infoModifyForm.getOldPwd();
        String newPwd = infoModifyForm.getNewPwd();
        
        // 取Session中登录用户信息
        HttpSession session = request.getSession();
        LoginUserInfo loginUserInfo = (LoginUserInfo) session.getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        
        if(null == loginUserInfo) {
            log.info("用户未登录进入InfoModifyAction，返回登录界面");
            response.sendRedirect(request.getContextPath() + "/admin/index.jsp");
            return null;
        }
        
        // 校验原始账号是否正确
        String account = loginUserInfo.getLoginName();
        boolean accountChkRs = loginService.checkUserLogin(account, oldPwd);
        
        if(!accountChkRs) {
            log.info("修改密码，原始账号或密码不正确.");
            // 登录错误信息
            ActionMessage errorMsg = new ActionMessage("chginfo.error.wrongpwd");
            ActionErrors errors = new ActionErrors();
            errors.add("chginfo.error.wrongpwd", errorMsg);
            addErrors(request, errors);
            
            return mapping.findForward("faild");
        }
        
        // 更新密码
        int updCnt = infoModifyService.updatePwd(account, newPwd);
        if(updCnt == 1) {
            log.debug("用户[" + account + "]更新密码成功");
            return mapping.findForward("success");
        }
        
        return mapping.findForward("faild");
    }

    public void setInfoModifyService(InfoModifyService infoModifyService) {
        this.infoModifyService = infoModifyService;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
    
}
